﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;

public class TweenTMProTextNum : UITweener
{
    public float from;
    public float to;


    TextMeshProUGUI mText;
    public TextMeshProUGUI cacheText { get { if (null == mText) { mText = gameObject.GetComponent<TextMeshProUGUI>(); } return mText; } }

    public bool m_isFormat = false;
    public string m_strFormat = string.Empty;

    [HideInInspector]
    public List<EventDelegate> onUpdate = new List<EventDelegate>();

    public float _value;
    /// <summary>
    /// Tween's current value.
    /// </summary>

    public float value
    {
        get
        {
            return _value;
        }
        set
        {
            _value = value;
            if (m_isFormat) { cacheText.text = string.Format(m_strFormat, (int)_value); }
            else { cacheText.text = ((int)_value).ToString(); }
        }
    }

    /// <summary>
    /// Tween the value.
    /// </summary>

    private List<EventDelegate> mTempOnUpdate = new List<EventDelegate>();

    protected override void OnUpdate(float factor, bool isFinished) {
        value = from * (1f - factor) + to * factor;

        if (null != onUpdate)
        {
            mTempOnUpdate = onUpdate;
            onUpdate = new List<EventDelegate>();

            // Notify the listener delegates
            EventDelegate.Execute(mTempOnUpdate);

            // Re-add the previous persistent delegates
            for (int i = 0; i < mTempOnUpdate.Count; ++i)
            {
                EventDelegate ed = mTempOnUpdate[i];
                if (ed != null && !ed.oneShot) EventDelegate.Add(onUpdate, ed, ed.oneShot);
            }
            mTempOnUpdate = null;
        }

    }

    /// <summary>
    /// Start the tweening operation.
    /// </summary>

    static public TweenTMProTextNum Begin(GameObject go, float duration, float startNum, float endNum )
    {
        TweenTMProTextNum comp = UITweener.Begin<TweenTMProTextNum>(go, duration);
        comp.from = startNum;
        comp.to = endNum;

        comp.m_isFormat = false;
        comp.m_strFormat = string.Empty;

        if (duration <= 0f)
        {
            comp.Sample(1f, true);
            comp.enabled = false;
        }
        return comp;
    }

    public void SetFormat(string strFormat)
    {
        m_isFormat = true;
        m_strFormat = strFormat;
    }

    ///// <summary>
    ///// Start the tweening operation.
    ///// </summary>

    //static public TweenPosition Begin (GameObject go, float duration, Vector3 pos)
    //{
    //	TweenPosition comp = UITweener.Begin<TweenPosition>(go, duration);
    //	//comp.worldSpace = worldSpace;
    //	comp.from = comp.value;
    //	comp.to = pos;

    //	if (duration <= 0f)
    //	{
    //		comp.Sample(1f, true);
    //		comp.enabled = false;
    //	}
    //	return comp;
    //}

    [ContextMenu("Set 'From' to current value")]
    public override void SetStartToCurrentValue() { from = value; }

    [ContextMenu("Set 'To' to current value")]
    public override void SetEndToCurrentValue() { to = value; }

    [ContextMenu("Assume value of 'From'")]
    void SetCurrentValueToStart() { value = from; }

    [ContextMenu("Assume value of 'To'")]
    void SetCurrentValueToEnd() { value = to; }

    /// <summary>
    /// Convenience function -- set a new OnFinished event delegate (here for to be consistent with RemoveOnFinished).
    /// </summary>

    public void SetOnUpdate(EventDelegate.Callback del) { EventDelegate.Set(onUpdate, del); }
}

